package com.he.springbootweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//处理登录的请求，来自index.html中表单的请求
@Controller
public class LoginController {

    @RequestMapping(value = "/user/login")
    //@RequestParam防止参数名与前端name名称不同，session存入信息，判断是否可以登录用
    public String login(@RequestParam("username") String username, @RequestParam("password") String password,
                        Model model, HttpSession session){

        //具体业务
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            session.setAttribute("loginUser",username);
            return "redirect:/main.html";
        }else {
            model.addAttribute("msg","用户名或者密码错误");
            return "/index";
        }
    }

    @RequestMapping(value = "/user/out")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }
}
